package com.mark.controller.portal;

import com.mark.common.Const;
import com.mark.common.ServerResponse;
import com.mark.controller.common.CurrentUser;
import com.mark.pojo.User;
import com.mark.service.IUserService;
import com.mark.util.CookieUtil;
import com.mark.util.JsonUtil;
import com.mark.util.RedisShardedPoolUtil;
import com.mark.vo.UserVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Created by Mark on 2018/3/22.
 * 用户模块Controller层
 */

@Controller
@RequestMapping("/user/")
public class UserController {
    @Autowired
    private IUserService iUserService;

    //1.用户注册
    @RequestMapping(value = "register.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<String> register(User user, HttpSession session, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse)  {
        if(user == null){
            return ServerResponse.createByIllegalArgument("用户信息不能为空");
        }
        ServerResponse<String> response = iUserService.register(user);
        if (response.isSuccess()) {///如果注册成功保存用户登录登录状态
            CurrentUser.setCurrentUser(httpServletRequest,httpServletResponse,session.getId(),user);
        }
        return response;
    }

    //2.用户登录
    @RequestMapping(value = "login.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<User> login(HttpSession session,String username,String password,HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse){
        if (StringUtils.isBlank(username) || StringUtils.isBlank(password)) {
            return ServerResponse.createByIllegalArgument("输入的用户名和密码不能为空");
        }
        ServerResponse<User> response = iUserService.login(username, password);
        if (response.isSuccess()) {//如果登录成功保存用户登录登录状态
            User currentUser = response.getData();
            CurrentUser.setCurrentUser(httpServletRequest,httpServletResponse,session.getId(),currentUser);
        }
        return response;
    }

    //3.退出登录
    @RequestMapping(value="logout.do" , method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<String> logout(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse){
        String loginToken = CookieUtil.readLoginToken(httpServletRequest);
        CookieUtil.delLoginToken(httpServletRequest,httpServletResponse);
        RedisShardedPoolUtil.del(loginToken);
        return  ServerResponse.createBySuccess();
    }

    //4.校验输入的用户名和邮箱校验
    @RequestMapping(value="check_valid.do" , method = RequestMethod.GET)
    @ResponseBody
    public ServerResponse<String> checkValid(String str,String type){
        if(StringUtils.isBlank(type)){
            return ServerResponse.createByIllegalArgument("输入的校验的类型不能为空");
        }
        return  iUserService.checkValid(str,type);
    }

    //5.判断用户是否已经登录
   @RequestMapping(value="check_user_info.do" , method = RequestMethod.POST)
    @ResponseBody
    public  ServerResponse<UserVo> checkLogin(HttpServletRequest httpServletRequest){
        ServerResponse response = CurrentUser.getCurrentUser(httpServletRequest);
        if (!response.isSuccess()){
            return ServerResponse.createByErrorMessage(response.getMsg());
        }
        User currentUser = (User) response.getData();
        UserVo userVo = new  UserVo();
        userVo = userVo.assembleUserVo(currentUser);
        return ServerResponse.createBySuccess(userVo);
    }

    //6.忘记密码时根据用户名找到问题
    @RequestMapping(value="forget_get_question.do" , method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<String> forgetGetQuestion(String username){
        if(StringUtils.isBlank(username)){
            return ServerResponse.createByIllegalArgument("输入的用户名不能为空");
        }
        return iUserService.selectQuestion(username);
    }

    //7.忘记密码时检查回答问题的答案
    @RequestMapping(value="forget_check_answer.do" , method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<String> forgetCheckAnswer(String username,String question,String answer){
        if(StringUtils.isBlank(username) || StringUtils.isBlank(question) || StringUtils.isBlank(answer)){
            return ServerResponse.createByIllegalArgument("输入的信息不能为空");
        }
        return iUserService.checkAnswer(username,question,answer);
    }

    //8.忘记密码时更新 新密码
    @RequestMapping(value = "forget_reset_password.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<String> forgetRestPassword(String username,String passwordNew,String forgetToken) {
        if(StringUtils.isBlank(username) || StringUtils.isBlank(passwordNew)){
            return ServerResponse.createByIllegalArgument("输入的信息不能为空");
        }
        if(StringUtils.isBlank(forgetToken)){
            return ServerResponse.createByException("Token 信息为空");
        }
        return iUserService.forgetResetPassword(username,passwordNew,forgetToken);
    }

    //9.登录状态下更新密码
    @RequestMapping(value="reset_password.do" , method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<String> resetPassword(HttpServletRequest httpServletRequest, String passwordNew,String passwordOld){
        if(StringUtils.isBlank(passwordNew) || StringUtils.isBlank(passwordOld)){
            return ServerResponse.createByIllegalArgument("输入的密码不能为空");
        }
        ServerResponse response = CurrentUser.getCurrentUser(httpServletRequest);
        if (!response.isSuccess()){
            return response;
        }
        User currentUser = (User) response.getData();
        return  iUserService.resetPassword(passwordOld,passwordNew,currentUser);
    }

    //10.更新个人信息、
    @RequestMapping(value="update_information.do" , method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<UserVo> updateInformation(HttpServletRequest httpServletRequest ,User user){
        if(user == null){
            return  ServerResponse.createByIllegalArgument("输入的用户信息不能为空");
        }
        ServerResponse response = CurrentUser.getCurrentUser(httpServletRequest);
        if (!response.isSuccess()){
            return response;
        }
        User currentUser = (User) response.getData();
        user.setId(currentUser.getId());
        user.setUsername(currentUser.getUsername());
        ServerResponse<UserVo> serverResponse = iUserService.updateInformation(user);
        if (serverResponse.isSuccess()) {
            String loginToken = CookieUtil.readLoginToken(httpServletRequest);
            serverResponse.getData().setUsername(currentUser.getUsername());
            RedisShardedPoolUtil.setEx(loginToken, JsonUtil.obj2String(serverResponse.getData()),Const.RedisCacheExtime.REDIS_SESSION_EXTIME);
        }
        return serverResponse;
    }

    //11.获取用户信息
    @RequestMapping(value="get_information.do" , method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<UserVo> getInformation(HttpServletRequest httpServletRequest){
        ServerResponse response = CurrentUser.getCurrentUser(httpServletRequest);
        if (!response.isSuccess()){
            return response;
        }
        User currentUser = (User) response.getData();
        if (currentUser != null){
            return iUserService.getInfo(currentUser.getId());
        }
        return ServerResponse.createByErrorMessage("用户未登录");
    }

    //12更新用户头像
    @RequestMapping(value="update_image.do" , method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse updateUserImageUrl(HttpServletRequest httpServletRequest, String imageUrl){
        ServerResponse response = CurrentUser.getCurrentUser(httpServletRequest);
        if (!response.isSuccess()){
            return response;
        }
        User currentUser = (User) response.getData();
        return iUserService.updateImageUrl(currentUser.getId(),imageUrl);
    }
}
